home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / drgnsmth.cpt / Dragonsmith 1.1 / Template files / DPrefsTmpl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-12  |  6.6 KB  |  185 lines

  1. /*
  2.     DPrefsTmpl.c
  3.     
  4.     Template for a simple Dragon sub-species with preferences settings and an added menu
  5.     
  6.     To base a simple dragon (let's call it "Tiamat") on the code in this file ╤
  7.     
  8.     1.    Make a copy of this file and rename it "DTiamat.c"
  9.     2.    Make a copy of the file "Template.╣" and name it "Tiamat.╣"
  10.     3.    Add DTiamat.c to the Tiamat.╣ project
  11.     4.    Change your project "creator" value to your dragon's signature
  12.     5.    Make a copy of the file "PrefsTmpl.╣.rsrc" and name it "Tiamat.╣.rsrc"
  13.     6.    Change all instances in this file of the string "PrefsTmpl" to "Tiamat"
  14.     7.    Move Tiamat.╣.rsrc up so it's in the same folder as Tiamat.╣
  15.     8.    Make the following changes in Tiamat.╣.rsrc ╤
  16.         ª Use ResEdit 2.1's 'BNDL' editor to change your dragon's signature and add an 'FREF' resource for each document
  17.             type the user can drag-and-drop on your dragon ('****' is all files, 'fold' is folders, and 'disk' is volumes)
  18.         ª Use ResEdit's icon family editor to change your dragon's icon ╤ a boring icon is provided for you to fiddle with
  19.         ª Change the values in 'DrPr' 128 (resolveAliases, dirDepthLimit, etc.), if you wish ╤ do NOT delete this resource
  20.         ª Edit the 'TEXT'/'styl' 128 resources ╤ they provide the Finder's help text for your dragon's icon (or remove these
  21.             two resources, as well as 'hfdr' ╨5696, if you'd rather not have this feature)
  22.         ª Change the value in 'MoMa' 128 if your dragon needs to call MoreMasters when it starts up
  23.         ª Edit the two 'vers' resources
  24.         ª Change the string in 'STR ' 128 to the name of your dragon's preferences file
  25.         ª Change the second entry in the 'PrRo' 128 resource so it designates your preferences resource
  26.     9.    Rewrite the method DTiamat::ProcessFile (or ProcessDirectory, or both) so your dragon actually does something
  27.     10.    Add other code as necessary
  28.     
  29.     Preferences that have been changed can be saved by calling preferences->SavePrefResource (prefPrefsTmplPrefs)
  30.         ╤ it's done for you at the end of DoOptionsMenu, but you can move the call if there's a more appropriate place for it
  31.     Adding additional preferences resources is easy ╤ just follow the example of prefPrefsTmplPrefs
  32.  
  33.     WARNING:    Do NOT call ReleaseResource or DetachResource on a preferences resource.  Instead, call
  34.                 preferences->ReleasePrefResource and preferences->DetachResource.
  35.  
  36.     Created    01 Oct 1992    Framework with Options menu and ReadPrefs
  37.     Modified    
  38.     
  39. */
  40.                                     
  41. #include    "Dragon.h"
  42.  
  43. enum {
  44.     mOptions = mEdit + 1
  45. };
  46.  
  47. enum {
  48.     // Put your Options menu item constants here
  49.     iNothingAtAll = 1
  50. };
  51.  
  52. enum {
  53.     // Put your preferences resource index constants here ╤ the first one should == prefDragonPrefs + 1
  54.     prefPrefsTmplPrefs = prefDragonPrefs + 1
  55. };
  56.  
  57. class DPrefsTmpl: public Dragon {
  58.     
  59.     protected:
  60.         MenuHandle        optionsMenu;            // Delete this instance variable if your dragon doesn't add any menus
  61.         Handle            templatePrefs;        // Change this to a custom struct or array or whatever you want
  62.         // Put instance variables for the preferences themselves here
  63.  
  64.     public:
  65.                         DPrefsTmpl (void);
  66.         virtual void        ProcessFile (void);
  67.         virtual void        ProcessDirectory (void);
  68.     
  69.     protected:
  70.         virtual void        SetUpMenus (void);
  71.         virtual void        DoMenu (long menuItemCode);
  72.         virtual void        DoOptionsMenu (short itemNum);
  73.         virtual void        AdjustMenusBusy (void);
  74.         virtual void        AdjustMenusIdle (void);
  75.         virtual void        ReadPrefs (void);
  76. };
  77.  
  78. Dragon *CreateGDragon (void)
  79. {
  80.     // This is a function, not a method.  It's declared in Dragon.h and is NOT defined in Dragon.c
  81.     
  82.     return (Dragon *) new DPrefsTmpl;
  83. }
  84.  
  85. DPrefsTmpl::DPrefsTmpl (void)
  86. {
  87.     // You should initialize any added instance variables here ╤ this method will be called whenever an object of
  88.     //    this class is created, immediately after Dragon::Dragon
  89.     
  90.     // Be especially careful to supply default values for all preferences here, in case the ReadPrefs method fails
  91.     //    (in low memory circumstances, for example)
  92.     
  93.     optionsMenu = NULL;
  94.     templatePrefs = NULL;
  95.     autoQuit = FALSE;        // This will be overridden by whatever value for autoQuit is specified in the 'DrPr' 128 resource in
  96.                         //    your project resource file ╤ so just removing this line won't necessarily be enough to make
  97.                         //    it auto-quit.  The file "PrefsTmpl.╣.rsrc" has a 'DrPr' 128 that sets autoQuit = FALSE
  98. }
  99.  
  100. void DPrefsTmpl::ProcessFile (void)
  101. {
  102.     // This is the heart of your dragon.  You can open the file, change its file attributes, or whatever ╔
  103.     // Simple changes can be simplified by using the macros defined in Dragon.h (curDocIsFile, curFileType, etc.)
  104.     
  105.     // Note that this method will not be called if you open a preferences file created by this dragon ╤ that's
  106.     //    all taken care of for you by Dragon (see ProcessOwnedFile)
  107. }
  108.  
  109. void DPrefsTmpl::ProcessDirectory (void)
  110. {
  111.     // Use this method to process folders and disks instead of just ignoring them or looking inside them for files
  112. }
  113.  
  114. void DPrefsTmpl::SetUpMenus (void)
  115. {
  116.     // Delete this method if your dragon doesn't add any menus (but then how will the user change the preferences settings?)
  117.     
  118.     inherited::SetUpMenus ();            // Add the Apple, File, and Edit menus
  119.  
  120.     optionsMenu = GetMenu (mOptions);
  121.     InsertMenu (optionsMenu, 0);
  122.     
  123.     DrawMenuBar ();
  124. }
  125.  
  126. void DPrefsTmpl::DoMenu (long menuItemCode)
  127. {
  128.     // Delete this method if your dragon doesn't add any menus
  129.     
  130.     short    menuID, itemNum;
  131.  
  132.     menuID = menuItemCode >> 16;
  133.     itemNum = menuItemCode & 0xFFFF;
  134.  
  135.     if (menuID == mOptions)
  136.         DoOptionsMenu (itemNum);
  137.     else
  138.         inherited::DoMenu (menuItemCode);
  139. }
  140.  
  141. void DPrefsTmpl::DoOptionsMenu (short itemNum)
  142. {
  143.     // Delete this method if your dragon doesn't add any menus
  144.     
  145.     switch (itemNum) {
  146.         // Put your case statements here╔
  147.         default:
  148.             break;
  149.     }
  150.     preferences->SavePrefResource (prefPrefsTmplPrefs);    // Delete this statement if you call SavePref elsewhere instead
  151. }
  152.  
  153. void DPrefsTmpl::AdjustMenusBusy (void)
  154. {
  155.     // Delete this method if your dragon doesn't add any menus
  156.     
  157.     inherited::AdjustMenusBusy ();
  158.     DisableItem (optionsMenu, 0);    // Disable the entire Options menu
  159. }
  160.  
  161. void DPrefsTmpl::AdjustMenusIdle (void)
  162. {
  163.     // Delete this method if your dragon doesn't add any menus
  164.     
  165.     inherited::AdjustMenusIdle ();
  166.     EnableItem (optionsMenu, 0);        // Enable what we disabled in AdjustMenusBusy ╤ this will NOT enable items that
  167.                                 //    were disabled to begin with
  168. }
  169.  
  170. void DPrefsTmpl::ReadPrefs (void)
  171. {
  172.     // Read in any needed prefs resources ╤ delete this method if your dragon doesn't add any preferences
  173.  
  174.     inherited::ReadPrefs ();
  175.     
  176.     // Note that you should NOT reference any preferences resource handles that may have been in memory
  177.     //    ╤ they will not be valid handles now because the file they were from has by this time been closed
  178.     
  179.     templatePrefs = (Handle) preferences->GetPrefResource (prefPrefsTmplPrefs);
  180.     if (templatePrefs != NULL) {
  181.         // Read data from the templatePrefs resource into instance variables
  182.     }
  183. }
  184.  
  185.